package com.gxuwz.zhangjiahui.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.zhangjiahui.entity.*;
import com.gxuwz.zhangjiahui.service.*;
import org.apache.ibatis.annotations.Param;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjiahui
 * @since 2021-06-20
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService iStudentService;
    @Autowired
    private IClassService iClassService;
    @Autowired
    private ICollegeService iCollegeService;


    /**
     * 查询全部信息
     * @param modelAndView
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @Pointcut
    @ResponseBody
    @RequestMapping(value = "/findStudent")
    public ModelAndView findStudent(ModelAndView modelAndView, Page page, Integer pageNumber, Student student,
                                    @Param("stuNo") String stuNo,
                                    HttpServletRequest request, HttpServletResponse response) {
        if(request.getSession().getAttribute("nowResult") != null){
            if (!request.getSession().getAttribute("nowResult").equals("now")) {
                request.getSession().setAttribute("result", "");
            }
            request.getSession().setAttribute("nowResult", "");
        }
        // 可以通过 wrapper 进行筛选!!!
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("stu_no");
        // 进行模糊查询!!!
        if(stuNo != null && stuNo != ""){
            wrapper.like("stu_no", stuNo);
            request.getSession().setAttribute("stuNo", stuNo);
        }else {
            request.getSession().setAttribute("stuNo", "");
        }
        // 根据团队编号进行筛选
        if(request.getSession().getAttribute("teamNo") != null){
            wrapper.eq("team_id", request.getSession().getAttribute("teamNo"));
        }
        // Current,页码 + Size,每页条数
        if(pageNumber == null){
            page.setCurrent(1);
        }else {
            page.setCurrent((long)pageNumber);
        }
        // 默认每页6行数据！
        page.setSize(6);
        // 调用分页查询方法！!
        IPage<Student> studentIPage = iStudentService.selectStudentPage(page, wrapper);
        // 存放page，内有当前页数
        modelAndView.addObject("page", page);
        System.out.println("总条数"+studentIPage.getTotal());
        System.out.println("总页数"+studentIPage.getPages());
        // 存放总页数
        modelAndView.addObject("pages", (int)studentIPage.getPages());
        // 存放一个数组用来让foreach遍历
        int[] pagesList = new int[(int)studentIPage.getPages()];
        for(int i=0; i< (int)studentIPage.getPages(); i++){
            pagesList[i] = i+1;
        }
        modelAndView.addObject("pagesList", pagesList);
        modelAndView.addObject("numberPages", studentIPage.getTotal());
        List<Student> studentList = studentIPage.getRecords();
        System.out.println("studentList = "+studentList);
        modelAndView.addObject("studentList", studentList);

        // 查询全部班级信息
        List<Classes> classList = iClassService.findClassAll();
        modelAndView.addObject("classList", classList);

        // 查询全部二级学院信息
        List<College> collegeList = iCollegeService.findCollegeAll();
        modelAndView.addObject("collegeList", collegeList);

        modelAndView.setViewName("student/student_list");
        return modelAndView;
    }



    /**
     * 添加/修改 信息
     * @param student
     * @return
     */
    @PostMapping(value = "/addEditStudent")
    public ModelAndView addEditStudent(Student student, HttpServletRequest request, ModelAndView modelAndView) {
        Student student1 = iStudentService.findStudentById(student);
        System.out.println("student = " + student);
        System.out.println("student1 = " + student1);
        // 新增信息
        if(student1 == null){
            System.out.println("进入新增");
            try {
                iStudentService.addStudent(student);
                request.getSession().setAttribute("result", "addTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "addFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        // 修改信息
        if(student1 != null){
            System.out.println("进入修改");
            try {
                iStudentService.updateStudentById(student);
                request.getSession().setAttribute("result", "editTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "editFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        modelAndView.setViewName("redirect:/student/findStudent");
        return modelAndView;
    }



    /**
     * 删除信息
     * @param stuNo
     * @return
     */
    @GetMapping(value = "/deleteStudent")
    public ModelAndView deleteStudent(HttpServletRequest request, ModelAndView modelAndView,
                             @Param("stuNo") String stuNo) {
        Student student = new Student();
        student.setStuNo(stuNo);
        try {
            iStudentService.deleteStudentById(student);
            request.getSession().setAttribute("result", "deleteTrue");
            request.getSession().setAttribute("nowResult", "now");
        }catch (Exception e){

        }
        modelAndView.setViewName("redirect:/student/findStudent");
        return modelAndView;
    }

}
